/* 
* Class name: OptionsDBAdapter *
* 
* Version info: v4.0 *
* 
* Copyright by Group4 from CmpE450 course *
*/ 

package options;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class OptionsDBAdapter {
	

	public static final String KEY_ID = "id";
	public static final String KEY_TYPE = "Type";
	public static final String KEY_LABEL = "Label";
	public static final String KEY_VALUE = "Value";

	private static final String DATABASE_NAME = "gamespacecore.db";
	private static final String DATABASE_TABLE = "Options";
	private static final int DATABASE_VERSION = 1;

	private static final String DATABASE_CREATE =
	        "CREATE TABLE  IF NOT EXISTS" + DATABASE_TABLE + " (" 
	        + KEY_ID + " integer primary key autoincrement, "
	        + KEY_TYPE + " text, " 
	        + KEY_LABEL + " text, " 
	        + KEY_VALUE + " text);";
	  
	
	private final Context context;
	private DatabaseHelper DBHelper;
	private SQLiteDatabase db;

	public OptionsDBAdapter(Context ctx) 
	{
	    this.context = ctx;
	    DBHelper = new DatabaseHelper(context);
	}
	     
	private static class DatabaseHelper extends SQLiteOpenHelper 
	{
		  
	    DatabaseHelper(Context context) 
	    {
	        super(context, DATABASE_NAME, null, DATABASE_VERSION);
	         
	    }  	   
	            
	    @Override
	    public void onCreate(SQLiteDatabase db) 
	    {         	
	        db.execSQL(DATABASE_CREATE);
	    }

	    @Override
	    public void onUpgrade(SQLiteDatabase db, int oldVersion, 
	                            int newVersion) 
	    {
	    	db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
	    	onCreate(db);
	    }
	} 
	    
	//opens the database
	public OptionsDBAdapter open() throws SQLException 
	{
	    db = DBHelper.getWritableDatabase();
	    return this;
	}
    //closes the database   
	public void close() 
	{
	    DBHelper.close();
	}
	        
	//insert an record into the database
	public long insertOption(Option o) 
	{	    
	    ContentValues initialValues = new ContentValues();
	    initialValues.put(KEY_TYPE, o.getType());
	    initialValues.put(KEY_LABEL, o.getLabel());
	    initialValues.put(KEY_VALUE, o.getValue()); 
	    return db.insert(DATABASE_TABLE, null, initialValues);
	}

	//updates a particular record
	public boolean updateOption(Option o) 
	{
		ContentValues initialValues = new ContentValues();
	    initialValues.put(KEY_TYPE, o.getType());
	    initialValues.put(KEY_LABEL, o.getLabel());
	    initialValues.put(KEY_VALUE, o.getValue()); 
	    return db.update(DATABASE_TABLE, initialValues, KEY_LABEL + "=" + o.getLabel(), null) > 0;
	}
	    
	//deletes a particular record
	public boolean deleteOption(Option o) 
	{
	    return db.delete(DATABASE_TABLE, KEY_LABEL + "=" + o.getLabel(), null) > 0;
	}

	//retrieves all records
	public Cursor getAllOptions() 
	{
		String orderby = KEY_TYPE +" ASC";
	    	
		Cursor c = db.query(DATABASE_TABLE, new String[] {
	    						KEY_ID,
	        		     		KEY_TYPE,
	        		     		KEY_LABEL,
	        		     		KEY_VALUE}, 
	        		     		null, null, null, null, orderby);

	    return c;
	}
}